<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>差错列表</cite></a>
    </span>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">差错列表</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-row" style="margin-bottom:15px;">
                    <div class="layui-form" style="float:right;">
                        <div class="layui-form-item" style="margin:0;">
                            <label class="layui-form-label">ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="channelMchId" id="channelMchId" autocomplete="off" class="layui-input">
                            </div>
                            <button id="search" class="layui-btn" data-type="reload">搜索</button>
                        </div>
                    </div>
                </div>
                <table id="chcList" lay-filter="chcList"></table>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="payBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> -->
</script>

<script>


    layui.use(['table','util','admin', 'form'], function(){
        var table = layui.table
        ,$ = layui.$
        ,form = layui.form
        ,admin = layui.admin
            ,view =layui.view
        ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息


        table.render({
            elem: '#chcList'
            ,url: layui.setter.baseUrl + '/bill/check_mistake/list'
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token
            }
            ,id: 'tableReload'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                    ,{field: 'channelMchId',title: '渠道商户ID'}
                    ,{field: 'batchNo',title: '对账批次号'}
                    ,{field: 'billDate', title: '账单时间', templet: '<div>{{ layui.util.toDateString(d.billDate, "yyyy-MM-dd") }}</div>' }
                    ,{field: 'bankType',title: '银行类型', template: '<div>{{ d.bankType == wxpay?"微信":"支付宝" }}</div>'}
                    ,{field: 'handleState',title: '类型状态', templet: '<div>{{ d.handleState == 0?"未处理":"已处理" }}</div>'}
                    ,{field:'createTime',  title: '创建时间', templet: '<div>{{ layui.util.toDateString(d.createTime) }}</div>' }
                    ,{field:'edite', title: '操作', toolbar: '#payBar'
                }
            ]]
            ,page: true
            ,skin: 'line'
        });



        //监听工具条
        table.on('tool(chcList)', function(obj){
            var data = obj.data;
            console.log(data)
            if(obj.event === 'detail'){

                var id =data.id;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/bill/check_mistake/get',
                    data: {
                        id: id
                    },
                    success: function(res){
                        if(res.code == 0){

                            admin.popup({
                                title: '差错消息详情'
                                , area: ['770px', '650px']
                                , id: 'LAY-popup-content-view'
                                , success: function (layero, index) {
                                    view(this.id).render('reconciliation/mistake/detail').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');

                                        var body =layer.getChildFrame('body',index);
                                        body.find('#email').val(data.email);
                                        form.val("layuiadmin-app-form-list",{

                                            "id":data.id,

                                        })
                                        $('#batchNo').val(res.data.batchNo);
                                        $('#billDate').val( layui.util.toDateString(res.data.billDate, "yyyy-MM-dd"));
                                        $('#billType').val(res.data.billType);
                                        $('#handleState').val(res.data.handleState==0?"未处理":"已处理");
                                        $('#bankType').val(res.data.bankType == "wxpay"?"微信":"支付宝");
                                        $('#channelMchId').val(res.data.channelMchId);
                                        $('#mistakeCount').val(res.data.mistakeCount);
                                        $('#unhandleMistakeCount').val(res.data.unhandleMistakeCount);
                                        $('#tradeCount').val(res.data.tradeCount);
                                        $('#bankTradeCount').val(res.data.bankTradeCount);
                                        $('#tradeAmount').val(res.data.tradeAmount);
                                        $('#bankTradeAmount').val(res.data.bankTradeAmount);
                                        $('#refundAmount').val(res.data.refundAmount);
                                        $('#bankRefundAmount').val(res.data.bankRefundAmount);
                                        $('#fee').val(res.data.fee);
                                        $('#bankFee').val(res.data.bankFee);
                                        $('#orgCheckFilePath').val(res.data.orgCheckFilePath);
                                        $('#releaseCheckFilePath').val(res.data.releaseCheckFilePath);
                                        $('#releaseState').val(res.data.releaseState ==0?"失败":"成功");
                                        $('#checkFailMsg').val(res.data.checkFailMsg);
                                        $('#bankErrMsg').val(res.data.bankErrMsg);
                                        $('#createTime').val( layui.util.toDateString(res.data.createTime));
                                        $('#updateTime').val( layui.util.toDateString(res.data.updateTime));
                                        form.render();
                                    });
                                }
                            });

                        }else {
                            layer.alert(res.msg,{
                                title: '请求成功，但是状态不对'
                            })
                        }
                    }
                })

            } else if(obj.event === 'edit'){

                var id = data.id;
                admin.popup({
                    title: '编辑'
                    , area: ['360px', '265px']
                    , id: 'LAY-popup-content-view'
                    , success: function (layero, index) {
                        view(this.id).render('reconciliation/mistake/update').done(function () {
                            form.render(null, 'layuiadmin-app-form-list');

                            form.on('submit(savBtn)',function(data){
                                console.log(data);
                                admin.req({
                                    type: 'post',
                                    url: layui.setter.baseUrl + '/bill/check_mistake/handle',
                                    data: {
                                        id: id,
                                        handleType: data.field.handleType,
                                        handleRemark: data.field.handleRemark
                                    },
                                    error: function(err){
                                        layer.alert(err.msg,{title: '请求失败'})
                                    },
                                    success: function(res){
                                        if(res.code == 0){
                                            layer.alert(res.msg,{title: "保存成功"},function(index){
                                                table.reload('tableReload');
                                                layer.closeAll();
                                            })
                                        }else{
                                            layer.alert('获取数据失败')
                                        }
                                    }
                                })
                                return false;
                            })

                            form.render();
                        });
                    }
                });
            }
        });

        // 事件处理
        var events = {
            del: function(othis, type){
                var checkState = table.checkState("tableReload")
                ,data = checkState.data; //获得选中的数据
                if(data.length === 0) return layer.msg('请选择数据');
                layer.confirm('确定删除选中的数据吗？', function(){
                    var userIds = "";
                    $.each(data, function(i, n){
                        userIds = userIds + "," + n.id;
                    });
                    console.log("userIds");
                    if(userIds != "") {
                        userIds = userIds.substring(1);
                    }
                    admin.req({
                        url: layui.setter.baseUrl + '/sys/message/delete',
                        data: {ids: userIds},
                        success: function(){
                            layer.msg('删除成功', {
                                icon: 1
                            });
                            table.reload("tableReload"); //刷新表格
                        }
                    });
                });
            }
        };
            // 搜索
        var $ = layui.$, active = {
            reload: function(){
                var channelMchId = $('#channelMchId').val();
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        channelMchId: channelMchId,
                    }
                });
            }
        };
        $('#search').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        $('.dele').on('click', function(){
            var othis = $(this)
                    ,thisEvent = othis.data('events')
                    ,type = othis.data('type');
            events[thisEvent] && events[thisEvent].call(this, othis, type);
        });
        // 渲染表单
        form.render();


    })
</script>
